package com.wanho.qz103.hrsaas.process.controller;

import com.wanho.qz103.hrsaas.commons.annontations.*;
import com.wanho.qz103.hrsaas.commons.exception.ControllerException;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.query.Page;
import com.wanho.qz103.hrsaas.commons.vo.R;
import com.wanho.qz103.hrsaas.commons.vo.TableVO;
import com.wanho.qz103.hrsaas.process.query.ProcessQuery;
import com.wanho.qz103.hrsaas.process.service.ProcessService;
import com.wanho.qz103.hrsaas.process.vo.DefinitionVO;
import com.wanho.qz103.hrsaas.process.vo.InstanceVO;
import com.wanho.qz103.hrsaas.process.vo.TaskVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 流程模块 控制器
 */
@RestController
public class ProcessController {
    @Autowired
    private ProcessService processService ;

    @PutMapping("/user/process/instance/commit")
    public R commit(HttpServletRequest request) throws ControllerException{
        try {
            boolean commit = processService.commit(request.getParameterMap());
            if (commit) {
                return R.success("申请成功!") ;
            }
            return R.fail("审批失败！") ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("审批异常！") ;
        }
    }

    /**
     * 查看流程定义列表  查询所有的流程定义
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/user/process/definition")
    public R getDefinitionList(HttpServletRequest request) throws ControllerException{
        try {
            List<DefinitionVO> definitionVOList = processService.findAllProcessDefinition();
            return R.success("查询流程定义列表成功",definitionVOList) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("查询流程定义列表异常！") ;
        }
    }

    /**
     * 根据 流程定义id  查询流程历史任务
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/user/process/instance/tasks/{id}")
    public R getTaskByProcessInstanceId(HttpServletRequest request) throws ControllerException{
        try {
            String requestURI = request.getRequestURI() ;
            String[] split = requestURI.split("/");
            String processInstanceId = split[split.length-1] ;
            List<TaskVO> taskVOList = processService.findTaskHistoryByProcessInstanceId(processInstanceId);
            return R.success("根据流程实例id获得历史信息成功!",taskVOList) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("根据流程实例id查询失败！！！") ;
        }
    }

    /**
     * 查询审批单的详情数据
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/user/process/instance/{id}")
    public R getProcessInstanceById(HttpServletRequest request) throws ControllerException{
        try {
            String requestURI = request.getRequestURI() ;
            String[] split = requestURI.split("/");
            String processInstanceId = split[split.length-1] ;
            InstanceVO instanceVO = processService.findProcessInstanceById(processInstanceId);
            return R.success("查看流程详情成功！",instanceVO) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("查看流程详情异常！") ;
        }
    }

    /**
     * 流程 列表 分页查询
     * @param request
     * @return
     * @throws ControllerException
     */
    @PutMapping("/user/process/instance/{page}/{pageSize}")
    public R listPage(HttpServletRequest request) throws ControllerException{
        try {
            //获得路径参数 页码  分页跨度
            String requestURI = request.getRequestURI();
            String[] split = requestURI.split("/");
            String page = split[split.length-2] ;
            String pageSize = split[split.length-1] ;
            Page page1 = new Page();
            if (StringUtils.isNotEmpty(page)){
                page1.setPage(Long.parseLong(page));
            }
            if (StringUtils.isNotEmpty(pageSize)){
                page1.setSize(Integer.parseInt(pageSize));
            }
            //获得请求参数
            ProcessQuery processQuery = new ProcessQuery();
            BeanUtils.populate(processQuery,request.getParameterMap());
            page1.setQuery(processQuery);
            TableVO tableVO = processService.findPage(page1);
            return  R.success("查询列表成功！",tableVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("分页查询列表!") ;
        }
    }

    /**
     * 启动流程
     * @param request
     * @return
     * @throws ControllerException
     */
    @PostMapping("/user/process/startProcess")
    public R  startProcess(HttpServletRequest request) throws ControllerException{
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            boolean startRs = processService.startProcess(parameterMap) ;
            if (startRs) {
                return R.success("启动流程成功！") ;
            }
            return R.fail("启动流程失败！") ;
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException("启动流程异常！") ;
        }
    }
}
